﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AconITF.Controls
{
	public class NumericUpDown : ScrollBar
	{
		static NumericUpDown()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(typeof(NumericUpDown)));
		}

		/// <summary>
		/// 权限等级
		/// </summary>
		public int AuthorityLevel
		{
			get => (int)GetValue(AuthorityLevelProperty);
			set => SetValue(AuthorityLevelProperty, value);
		}

		public static readonly DependencyProperty AuthorityLevelProperty =
			DependencyProperty.Register("AuthorityLevel", typeof(int), typeof(NumericUpDown), new PropertyMetadata(1, OnAuthorityLevelChanged));
		/// <summary>
		/// 修改权限时触发 设置 IsEnabled 的值
		/// </summary>
		/// <param name="d"></param>
		/// <param name="e"></param>
		private static void OnAuthorityLevelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			NumericUpDown obj = d as NumericUpDown;
			int value = (int)obj.GetValue(AuthorityLevelMinProperty);
			obj.SetValue(IsEnabledProperty, (int)e.NewValue >= value);
		}

		/// <summary>
		/// 最低使用权限
		/// </summary>
		public int AuthorityLevelMin
		{
			get => (int)GetValue(AuthorityLevelMinProperty);
			set => SetValue(AuthorityLevelMinProperty, value);
		}

		public static readonly DependencyProperty AuthorityLevelMinProperty =
			DependencyProperty.Register("AuthorityLevelMin", typeof(int), typeof(NumericUpDown), new PropertyMetadata(1, OnAuthorityLevelMinChanged));
		/// <summary>
		/// 修改权限时触发 设置 IsEnabled 的值
		/// </summary>
		/// <param name="d"></param>
		/// <param name="e"></param>
		private static void OnAuthorityLevelMinChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			NumericUpDown obj = d as NumericUpDown;
			int value = (int)obj.GetValue(AuthorityLevelProperty);
			obj.SetValue(IsEnabledProperty, value >= (int)e.NewValue);
		}


		/// <summary>
		/// 数据精度
		/// </summary>
		public int Precision
		{
			get => (int)GetValue(PrecisionProperty);
			set => SetValue(PrecisionProperty, value);
		}

		public static readonly DependencyProperty PrecisionProperty =
			DependencyProperty.Register("Precision", typeof(int), typeof(NumericUpDown), new PropertyMetadata(2));


		/// <summary>
		/// 应用控件模板时调用
		/// </summary>
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			PART_TextBox = GetTemplateChild("PART_TextBox") as TextBox;
			if (GetTemplateChild("PART_ButtonUp") is Button btn_up
				&& GetTemplateChild("PART_ButtonDown") is Button btn_down
				&& GetTemplateChild("PART_Border") is Border border)
			{
				btn_up.Click += Btn_up_Click;
				btn_down.Click += Btn_down_Click;
				border.MouseWheel += Border_MouseWheel;
			}
		}

		TextBox PART_TextBox;
		private void Border_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
		{
			Value = e.Delta > 0
				? Value + LargeChange > Maximum ? Math.Round(Maximum, Precision) : Math.Round(Value + LargeChange, Precision)
				: Value - LargeChange < Minimum ? Math.Round(Minimum, Precision) : Math.Round(Value - LargeChange, Precision);
			PART_TextBox.SelectionStart = PART_TextBox.Text.Length;
		}

		private void Btn_down_Click(object sender, RoutedEventArgs e)
		{
			Value = Value - SmallChange < Minimum ? Math.Round(Minimum, Precision) : Math.Round(Value - SmallChange, Precision);
			PART_TextBox.ScrollToEnd();
			PART_TextBox.SelectionStart = PART_TextBox.Text.Length;
		}

		private void Btn_up_Click(object sender, RoutedEventArgs e)
		{
			Value = Value + SmallChange > Maximum ? Math.Round(Maximum, Precision) : Math.Round(Value + SmallChange, Precision);
			PART_TextBox.ScrollToEnd();
			PART_TextBox.SelectionStart = PART_TextBox.Text.Length;
		}
	}
}
